<template>
  <div class="right-content">
    <div class="search">
      <el-button type="primary" @click="handleAdd" size="small" v-hasPermi="['editor:documentBlockInfo:add']">新增</el-button>
      <div class="btns">
        <el-input
          clearable
          v-model="keyword"
          placeholder="请输入关键字"
          style="width: 300px"
          maxlength="100"
          size="small"
          @keyup.enter.native="handleQuery"
          @clear="resetQuery"
        />
        <el-button class="search-btn" type="primary" size="small" @click="handleQuery">搜索</el-button>
        <el-button type="primary" plain size="small" @click="resetQuery">重置</el-button>
      </div>
    </div>
    <div class="list-wrapper">
      <el-table :data="dataList" v-loading="loading" style="width: 100%">
        <el-table-column label="序号" type="index" width="50" :index="indexMethod"/>
        <el-table-column prop="name" label="语料名称" />
        <el-table-column prop="intro" label="语料描述" />
        <el-table-column prop="createByName" label="创建人" />
        <el-table-column prop="createDept" label="部门" />
        <el-table-column label="状态" width="100">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.isPublished"
              size="mini"
              v-hasPermi="['editor:documentBlockInfo:change']"
              @change="switchStatus(scope.row)"
            />
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200">
          <template slot-scope="scope">
            <el-button type="text" size="mini" @click="handleEdit(scope.row)" v-hasPermi="['editor:documentBlockInfo:editInfo']"
              >编辑信息</el-button
            >
            <el-button type="text" size="mini" @click="handleEditContent(scope.row)" v-hasPermi="['editor:documentBlockInfo:editContent']"
              >编辑正文</el-button
            >
            <el-button type="text" size="mini" @click="handleDelete(scope.row)" v-hasPermi="['editor:documentBlockInfo:remove']"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        class="business-pager p20 fr"
        v-show="dataTotal > 0"
        :background="true"
        :current-page.sync="queryParams.pageNum"
        :page-size.sync="queryParams.pageSize"
        :layout="'total, sizes, prev, pager, next, jumper'"
        :page-sizes="[10, 30, 50, 100]"
        :pager-count="7"
        :total="dataTotal"
        @size-change="handleSizeChange"
        @current-change="handlepageNumChange"
      />
    </div>
    <BlockEditModal
      :visible.sync="editModalShow"
      :editData="editData"
      @onClose="onCloseDialog"
      :selectedNode="selectedNode"
      @onSuccess="onSuccess"
    />
  </div>
</template>
<script>
import { Message, MessageBox } from 'element-ui'
import BlockEditModal from './BlockEditModal'
import { getBlocksList, getBlockInfo, deleteBlockInfo, handleBlockStatus } from '@/api/business'

export default {
  name: 'RightContent',
  components: { BlockEditModal },
  props: {
    selectedNode: {
      type: Object,
      default: () => ({})
    }
  },
  watch: {
    selectedNode(val) {
      this.onSearch()
    }
  },
  data() {
    return {
      editModalShow: false,
      editData: null,
      keyword: '',
      queryParams: {
        pageNum: 1,
        pageSize: 10
      },
      dataTotal: 0,
      dataList: [],
      loading: false
    }
  },
  mounted() {
    this.handleQuery()
  },
  methods: {
    indexMethod(index) {
      return (
        (this.queryParams.pageNum - 1) * this.queryParams.pageSize + index + 1
      );
    },
    handleAdd() {
      if (!this.selectedNode.id) {
        return Message.error('请先选择分类节点')
      }
      this.editData = null
      this.editModalShow = true
    },
    resetQuery() {
      this.keyword = ''
      this.handleQuery()
    },
    handleQuery() {
      this.queryParams.current = 1
      this.onSearch()
    },
    onSearch() {
      this.loading = true
      getBlocksList({
        keyword: this.keyword,
        classifyId: this.selectedNode?.id,
        pageNum: this.queryParams.pageNum,
        pageSize: this.queryParams.pageSize
      })
        .then((res) => {
          this.dataList = res.data.list.map((item) => ({
            ...item,
            isPublished: item.status === '1'
          }))
          this.dataTotal = res.data.count
        })
        .finally(() => {
          this.loading = false
        })
    },
    handleEdit(_data) {
      getBlockInfo(_data.id).then((res) => {
        this.editData = res.data
        this.editModalShow = true
      })
    },
    handleEditContent(item) {
      window.open(`/editor/view?id=${item.id}&editContentType=block`, '_blank')
    },
    handleDelete(item) {
      MessageBox.confirm(`确认删除内容块"${item.name}"吗？删除后不可恢复`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        customClass: 'business-dialog',
        beforeClose: (action, instance, done) => {
          if (action === 'confirm') {
            instance.confirmButtonLoading = true
            deleteBlockInfo(item.id)
              .then((res) => {
                Message.success('删除成功')
                this.onSearch()
              })
              .finally(() => {
                instance.confirmButtonLoading = false
                done()
              })
          } else {
            done()
          }
        }
      })
        .then(() => {})
        .catch((err) => {
          console.log(err)
        })
    },
    onCloseDialog() {
      this.editData = null
    },
    onSuccess() {
      this.editData = null
      this.handleQuery()
    },
    switchStatus(item) {
      handleBlockStatus({
        id: item.id,
        status: item.isPublished ? '1' : '0'
      })
        .then((res) => {
          Message.success('操作成功')
        })
        .finally(() => {
          this.onSearch()
        })
    },
    handleSizeChange(val) {
      if (this.queryParams.pageSize * val > this.dataTotal) {
        this.queryParams.pageNum = 1
      }
      this.handleQuery()
    },
    handlepageNumChange() {
      this.handleQuery()
    }
  }
}
</script>
<style lang="scss" scoped>
@import "~@/assets/styles/editor.scss";
.editor-business-page .right-content .list-wrapper {
  height: calc(100vh - 200px);
}
</style>
